#! /usr/bin/perl

use strict;

$ENV{MAUDE_LIB} = "/home/software/maude22";
my $maudepath = "/home/software/maude22/maude.linux";
my $pgmfile = "/home/software/javamop2.0/javamop/plugins/FTLTL/bttFsm2.maude";

my $formula ;
my $res = "";

my $filename;
my $dot_input;
my $html_output;
my $maude_input;
my %event_names;

create_maude_input();
exec_maude();
create_output();
print $html_output;
#print "\n\<dot\>\n";
#print $dot_input."\n";

sub create_maude_input {
    my $elt;
    my $ops = "";
    while (<>) {
	    if (/^\s*[Ee]vent\s+([^\t <]+)[^:]*:/) {
	    	$elt = $1;
            $elt =~ s/_//g;   
            $ops = $ops."  op $elt : -> Atom .\n";
	    }
		#hack so that we don't have to have :
		#but still can have : for input from the main
		#JavaMOP trial where we don't want to have to strip
		#off the pointcuts 
		elsif (/^\s*[Ee]vent\s+([^\t <]+)/) {
	    	my $orig_elt = trim($1);
			$elt = $orig_elt;
            $elt =~ s/_//g;   
            $event_names{$elt} = $orig_elt;
            $ops = $ops."  op $elt : -> Atom .\n";
	    }
	    elsif (/^\s*[Ff]tltl\s*:\s*(.*)/) {
		    $formula = $1;
			$formula =~ s/_//g;
	    }
    }    
    $formula =~ s/[\r\n]/ /g;
    my $f = $formula;
    $f =~ s/;//g;
	  $f =~ s/\!|\/\\|\\\/|\-\>|\<\-\>|\+\+|\{\*\}|\[\]|\<\>/ $& /g;
	  $f =~ s/\s+/ /g;

    $maude_input = "in $pgmfile\n";
    $maude_input = $maude_input."fmod TEST is pr BTT-FSM . \n";
    $maude_input = $maude_input.$ops;
    $maude_input = $maude_input."op formula : -> Formula . \n \n";
    $maude_input = $maude_input."eq formula = $f . \n";
    $maude_input = $maude_input."endfm \n \n";
    $maude_input = $maude_input."red bttFsm(formula) . \n\n quit .\n";
}

sub exec_maude{

    my $i = 0;
    while (-e "/tmp/ftltl.out$i") {
        $i++;
    }

    open MAUDEIN, "|$maudepath > /tmp/ftltl.out$i" || die "Cannot execute $maudepath\n";
    print MAUDEIN $maude_input;
    close MAUDEIN;

    open MAUDEOUT, "</tmp/ftltl.out$i" || die "Cannot open /tmp/ftltl.out$i\n";
    my $flag = 0;
    my $line;
    while($line = <MAUDEOUT>){
        chomp($line);
        if($line =~ /^result BttFsm: (.*)$/){
            $res = $1;
            $flag = 1;
        }
        else {
            if($line =~ /^Bye/) {
                $flag = 0;
            }
            if($flag) {
                $res = $res.$line;
            }}
    }
    close(MAUDEOUT);
    system("rm -f /tmp/ftltl.out$i");
}

sub create_output{
	  my $k;
	  foreach $k (keys %event_names){
		$res =~ s/$k/$event_names{$k}/g;
	  }
	  my @output = split(/,/, $res);
	  my @btt;
	  my $count = 0;
	  my $output;
	  my $btt;
	  foreach $output(@output){	  	
	  	if ($output =~ /\s*[\(]?\s*\d\[(.*)\]\s*\[.*/) {
	  		$btt[$count] = $1;
	  		$btt[$count] =~ s/\bf\b/ false /;
	  		$btt[$count] =~ s/\bt\b/ true /;
	  	}
	  	$count ++;
	  }

    my $edges = "";
 
	$html_output = "<pre>\n";
    $html_output .= "Starting state: 1\n";
    $html_output .= "Transitions:\n";
    $html_output .= "switch (state) {\n";
    $count = 1;
    foreach $btt(@btt){
    	$html_output = $html_output."case $count: state <- ".$btt."\n";
    	$count++;
    }
    $html_output = $html_output."}\n</pre>";
    $html_output .= "<INPUT TYPE='button' value='Output Syntax Help'" ;
    $html_output .=
          " onclick='javascript:window.open(\"http://fsl.cs.uiuc.edu/index.php/FTLTL_Plugin_Output_Syntax\")'>" ;
 
}

sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
